﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Catering_DBOpertion;//Please add references
namespace Catering_Operation
{
    /// <summary>
    /// 数据访问类:InDbInfo
    /// </summary>
    public partial class InDbInfo
    {
        public InDbInfo()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "InDbInfo");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from InDbInfo");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.InDbInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into InDbInfo(");
            strSql.Append("id,GroupID,ClassID,DBID,IntDate,PiaoDate,RAWID,RAW,Specifications,Number,CMP,AmountMoney,Weight,RAWTID,RAWIDS,Back,temp1,temp2,InDBid,Price,SumNumber)");
            strSql.Append(" values (");
            strSql.Append("@id,@GroupID,@ClassID,@DBID,@IntDate,@PiaoDate,@RAWID,@RAW,@Specifications,@Number,@CMP,@AmountMoney,@Weight,@RAWTID,@RAWIDS,@Back,@temp1,@temp2,@InDBid,@Price,@SumNumber)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@GroupID", SqlDbType.Int,4),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@DBID", SqlDbType.Int,4),
					new SqlParameter("@IntDate", SqlDbType.DateTime),
					new SqlParameter("@PiaoDate", SqlDbType.DateTime),
					new SqlParameter("@RAWID", SqlDbType.Int,4),
					new SqlParameter("@RAW", SqlDbType.VarChar,100),
					new SqlParameter("@Specifications", SqlDbType.VarChar,100),
					new SqlParameter("@Number", SqlDbType.VarChar,100),
					new SqlParameter("@CMP", SqlDbType.VarChar,100),
					new SqlParameter("@AmountMoney", SqlDbType.Decimal,9),
					new SqlParameter("@Weight", SqlDbType.VarChar,100),
					new SqlParameter("@RAWTID", SqlDbType.Int,4),
					new SqlParameter("@RAWIDS", SqlDbType.Int,4),
					new SqlParameter("@Back", SqlDbType.VarChar,200),
					new SqlParameter("@temp1", SqlDbType.VarChar,200),
					new SqlParameter("@temp2", SqlDbType.VarChar,200),
					new SqlParameter("@InDBid", SqlDbType.VarChar,50),
					new SqlParameter("@Price", SqlDbType.VarChar,50),
                    new SqlParameter("@SumNumber", SqlDbType.Decimal,20)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.GroupID;
            parameters[2].Value = model.ClassID;
            parameters[3].Value = model.DBID;
            parameters[4].Value = model.IntDate;
            parameters[5].Value = model.PiaoDate;
            parameters[6].Value = model.RAWID;
            parameters[7].Value = model.RAW;
            parameters[8].Value = model.Specifications;
            parameters[9].Value = model.Number;
            parameters[10].Value = model.CMP;
            parameters[11].Value = model.AmountMoney;
            parameters[12].Value = model.Weight;
            parameters[13].Value = model.RAWTID;
            parameters[14].Value = model.RAWIDS;
            parameters[15].Value = model.Back;
            parameters[16].Value = model.temp1;
            parameters[17].Value = model.temp2;
            parameters[18].Value = model.InDBid;
            parameters[19].Value = model.Price;
            parameters[20].Value = model.SumNumber;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.InDbInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update InDbInfo set ");
            strSql.Append("GroupID=@GroupID,");
            strSql.Append("ClassID=@ClassID,");
            strSql.Append("DBID=@DBID,");
            strSql.Append("IntDate=@IntDate,");
            strSql.Append("PiaoDate=@PiaoDate,");
            strSql.Append("RAWID=@RAWID,");
            strSql.Append("RAW=@RAW,");
            strSql.Append("Specifications=@Specifications,");
            strSql.Append("Number=@Number,");
            strSql.Append("CMP=@CMP,");
            strSql.Append("AmountMoney=@AmountMoney,");
            strSql.Append("Weight=@Weight,");
            strSql.Append("RAWTID=@RAWTID,");
            strSql.Append("RAWIDS=@RAWIDS,");
            strSql.Append("Back=@Back,");
            strSql.Append("temp1=@temp1,");
            strSql.Append("temp2=@temp2,");
            strSql.Append("InDBid=@InDBid,");
            strSql.Append("Price=@Price");
            strSql.Append("SumNumber=@SumNumber");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@GroupID", SqlDbType.Int,4),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@DBID", SqlDbType.Int,4),
					new SqlParameter("@IntDate", SqlDbType.DateTime),
					new SqlParameter("@PiaoDate", SqlDbType.DateTime),
					new SqlParameter("@RAWID", SqlDbType.Int,4),
					new SqlParameter("@RAW", SqlDbType.VarChar,100),
					new SqlParameter("@Specifications", SqlDbType.VarChar,100),
					new SqlParameter("@Number", SqlDbType.VarChar,100),
					new SqlParameter("@CMP", SqlDbType.VarChar,100),
					new SqlParameter("@AmountMoney", SqlDbType.Decimal,9),
					new SqlParameter("@Weight", SqlDbType.VarChar,100),
					new SqlParameter("@RAWTID", SqlDbType.Int,4),
					new SqlParameter("@RAWIDS", SqlDbType.Int,4),
					new SqlParameter("@Back", SqlDbType.VarChar,200),
					new SqlParameter("@temp1", SqlDbType.VarChar,200),
					new SqlParameter("@temp2", SqlDbType.VarChar,200),
					new SqlParameter("@InDBid", SqlDbType.VarChar,50),
					new SqlParameter("@Price", SqlDbType.VarChar,50),
                    new SqlParameter("@SumNumber", SqlDbType.Decimal,20),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.GroupID;
            parameters[1].Value = model.ClassID;
            parameters[2].Value = model.DBID;
            parameters[3].Value = model.IntDate;
            parameters[4].Value = model.PiaoDate;
            parameters[5].Value = model.RAWID;
            parameters[6].Value = model.RAW;
            parameters[7].Value = model.Specifications;
            parameters[8].Value = model.Number;
            parameters[9].Value = model.CMP;
            parameters[10].Value = model.AmountMoney;
            parameters[11].Value = model.Weight;
            parameters[12].Value = model.RAWTID;
            parameters[13].Value = model.RAWIDS;
            parameters[14].Value = model.Back;
            parameters[15].Value = model.temp1;
            parameters[16].Value = model.temp2;
            parameters[17].Value = model.InDBid;
            parameters[18].Value = model.Price;
            parameters[19].Value = model.SumNumber;
            parameters[20].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from InDbInfo ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from InDbInfo ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteByWhere(string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from InDbInfo ");
            strSql.Append(" where " + where);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.InDbInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,GroupID,ClassID,DBID,IntDate,PiaoDate,RAWID,RAW,Specifications,Number,CMP,AmountMoney,Weight,RAWTID,RAWIDS,Back,temp1,temp2,InDBid,Price,SumNumber from InDbInfo ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Catering_Model.InDbInfo model = new Catering_Model.InDbInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["GroupID"].ToString() != "")
                {
                    model.GroupID = int.Parse(ds.Tables[0].Rows[0]["GroupID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ClassID"].ToString() != "")
                {
                    model.ClassID = int.Parse(ds.Tables[0].Rows[0]["ClassID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DBID"].ToString() != "")
                {
                    model.DBID = int.Parse(ds.Tables[0].Rows[0]["DBID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IntDate"].ToString() != "")
                {
                    model.IntDate = DateTime.Parse(ds.Tables[0].Rows[0]["IntDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PiaoDate"].ToString() != "")
                {
                    model.PiaoDate = DateTime.Parse(ds.Tables[0].Rows[0]["PiaoDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWID"].ToString() != "")
                {
                    model.RAWID = int.Parse(ds.Tables[0].Rows[0]["RAWID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAW"] != null)
                {
                    model.RAW = ds.Tables[0].Rows[0]["RAW"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Specifications"] != null)
                {
                    model.Specifications = ds.Tables[0].Rows[0]["Specifications"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Number"] != null)
                {
                    model.Number = ds.Tables[0].Rows[0]["Number"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CMP"] != null)
                {
                    model.CMP = ds.Tables[0].Rows[0]["CMP"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AmountMoney"].ToString() != "")
                {
                    model.AmountMoney = decimal.Parse(ds.Tables[0].Rows[0]["AmountMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Weight"] != null)
                {
                    model.Weight = ds.Tables[0].Rows[0]["Weight"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RAWTID"].ToString() != "")
                {
                    model.RAWTID = int.Parse(ds.Tables[0].Rows[0]["RAWTID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWIDS"].ToString() != "")
                {
                    model.RAWIDS = int.Parse(ds.Tables[0].Rows[0]["RAWIDS"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Back"] != null)
                {
                    model.Back = ds.Tables[0].Rows[0]["Back"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp1"] != null)
                {
                    model.temp1 = ds.Tables[0].Rows[0]["temp1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp2"] != null)
                {
                    model.temp2 = ds.Tables[0].Rows[0]["temp2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["InDBid"] != null)
                {
                    model.InDBid = ds.Tables[0].Rows[0]["InDBid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Price"] != null)
                {
                    model.Price = ds.Tables[0].Rows[0]["Price"].ToString();
                }
                if (!(ds.Tables[0].Rows[0]["SumNumber"] is DBNull))
                {
                    model.SumNumber = Convert.ToDecimal(ds.Tables[0].Rows[0]["SumNumber"]);
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,GroupID,ClassID,DBID,IntDate,PiaoDate,RAWID,RAW,Specifications,Number,CMP,AmountMoney,Weight,RAWTID,RAWIDS,Back,temp1,temp2,InDBid,Price,SumNumber ");
            strSql.Append(" FROM InDbInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,GroupID,ClassID,DBID,IntDate,PiaoDate,RAWID,RAW,Specifications,Number,CMP,AmountMoney,Weight,RAWTID,RAWIDS,Back,temp1,temp2,InDBid,Price,SumNumber ");
            strSql.Append(" FROM InDbInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 修改结余量
        /// </summary>
        public bool SetJieYu(decimal jieyu, int id)
        {
            string strSQL = string.Format("update InDbInfo set temp1='{0}' where id={1}", jieyu.ToString(), id.ToString());
            return DbHelperSQL.ExecuteSql(strSQL) > 0;
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "InDbInfo";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("GetPageData", parameters, "InDbInfo");
        }

        /// <summary>
        /// 根据条件获取数据总条数
        /// </summary>
        public int GetCountBywhere(string where)
        {
            string strSql = "";
            if (where != "")
            {
                strSql = string.Format("select count(*) from InDbInfo where {0}", where);
            }
            else
            {
                strSql = "select count(*) from InDbInfo";
            }
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql));
        }

        /// <summary>
        /// 根据日期获取台帐
        /// </summary>
        public DataSet GetTZ(string strDatetime, int ClassID, int RawID)
        {
            StringBuilder sb = new StringBuilder();
            DateTime tempDate = DateTime.MinValue;
            tempDate=DateTime.Parse(strDatetime);
            string time1 = "";
            string time2 = "";
            if (tempDate > DateTime.MinValue)
            {
                time1 = tempDate.ToString("yyyy-MM-dd");
                time2 = tempDate.AddDays(1).ToString("yyyy-MM-dd");
            }

            sb.Append("select(");
            //--得到某一天的 入库数量
            sb.Append("select SUM(convert(decimal(18,2),Number)) from InDbInfo ");
            sb.Append("where ");
            sb.Append("IntDate >='" + time1 + "' ");
            sb.Append("and IntDate<'" + time2 + "' ");
            sb.Append("and ClassID=" + ClassID + " ");
            sb.Append("and RAWID=" + RawID + " ");
            sb.Append("group by RAWID ");
            sb.Append(") as InNumber ");
            //--得到某一天的 出库数量
            sb.Append(",( ");
            sb.Append("select SUM(convert(decimal(18,2),Numbers)) from FromRecords ");
            sb.Append("where ");
            sb.Append("OutDate >='" + time1 + "' ");
            sb.Append("and OutDate<'" + time2 + "' ");
            sb.Append("and ClassID=" + ClassID + " ");
            sb.Append("and RAWID=" + RawID + " ");
            sb.Append("group by RAWID ");
            sb.Append(") as OutNumber ");
            //--得到一天的结余量
            sb.Append(",( ");
            sb.Append("select top 1 balance from Balance ");
            sb.Append("where ");
            //sb.Append("IntDate >=convert(datetime,'" + strDatetime + "') ");and 
            sb.Append("AddDate<'" + time2 + "' ");
            sb.Append("and ClassID=" + ClassID + " ");
            sb.Append("and RAWID=" + RawID + " ");
            sb.Append("order by AddDate desc ");
            sb.Append(") as jieyu ");

            return DbHelperSQL.Query(sb.ToString());
        }

        #endregion  Method
        /// <summary>
        /// 获取采购口径
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public DataTable GetInDbInfoMoneyCount(string where)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"SELECT SUM(Convert(decimal(18,2),Number)*Convert(decimal(18,2),Price)) AS AmountMoney FROM view_InDbInfo where 1=1");
            if (!string.IsNullOrEmpty(where))
            {
                sql.Append(where);
            }
            return DbHelperSQL.Query(sql.ToString()).Tables[0];
        }

        #region 入库状态

        public DataTable GetDataStateByInDbInfoNew(string where)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT * FROM View_DataStates03 ");
            if (!string.IsNullOrEmpty(where)) sql.Append("WHERE " + where);
            return DbHelperSQL.Query(sql.ToString()).Tables[0];
        }

        #endregion
    }
}

